package com.ruoyi.yljf.controller.AppController;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.security.annotation.Anonymous;
import com.ruoyi.yljf.domain.Jingfang;
import com.ruoyi.yljf.domain.dto.CommonTypeDTO;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.DrugVO;
import com.ruoyi.yljf.domain.vo.JingfangVO;
import com.ruoyi.yljf.service.ICollectRecordService;
import com.ruoyi.yljf.service.IDrugCategoryService;
import com.ruoyi.yljf.service.IJingfangService;
import com.ruoyi.yljf.util.CommonUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 经方
 */
@Anonymous
@RestController
@RequestMapping("api/jingfang")
public class ApiJingfangController extends BaseController {

    @Resource
    private IDrugCategoryService drugCategoryService;
    @Resource
    private IJingfangService jingfangService;
    @Resource
    private ICollectRecordService collectRecordService;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 经方分类列表
     */
    @GetMapping("category/list")
    public AjaxResult getDrugCategory() {
        List list = drugCategoryService.getDrugCategory();
        return success(list);
    }

    /**
     * 查询经方列表
     */
    @GetMapping("/list")
    public AjaxResult getJingFangList(CommonTypeDTO dto) {
        return success(jingfangService.getJingfangVO(dto));
    }

    /**
     * 查询经方列表V2
     */
    @GetMapping("/listV2")
    public AjaxResult getJingFangV2List(CommonTypeDTO dto) {
        return success(jingfangService.getJingfangV2VO(dto));
    }

    /**
     * 根据id查询经方详情
     */
    @GetMapping("/getJingfangById")
    public AjaxResult getDrugById(@RequestParam Long id, HttpServletRequest req) {
        Jingfang byId = jingfangService.getById(id);
        JingfangVO jingfangVO = BeanUtil.copyProperties(byId, JingfangVO.class);
        if(jingfangVO!=null && commonUtils.userIfLogin(req))
            jingfangVO.setCollect(collectRecordService.ifCollect(yljfEnums.NOTICE_TYPE_JINGFANG.getCode(),id,getUserId()));
        return success(jingfangVO);
    }

}
